/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#include "GimboSettingsFrame.h"
#include <wx/msgdlg.h>

DEFINE_EVENT_TYPE(MY_EVT_SETTINGS_CHANGED)

GimboSettingsFrame::GimboSettingsFrame( wxWindow* parent, TGimboSettings *settings )
:
SettingsFrame( parent ),
settings(settings)
{
  //set field values
  if (settings)
  {
    checkBox_Autoconnect->SetValue(settings->Autoconnect);
    comboBox_Hub->SetValue(settings->Hub);
    
    std::vector<std::string>::iterator i;
    for (i=settings->Hubs.begin();i!=settings->Hubs.end();i++)
    {
      comboBox_Hub->Insert(*i,0);
    }
    
    textCtrl_Port->SetValue(wxString::Format("%d",settings->Port));
    textCtrl_OwnName->SetValue(settings->OwnName);
    checkBox_Loop1->SetValue(settings->Loop1Enabled);
    checkBox_Loop2->SetValue(settings->Loop2Enabled);
    textCtrl_Timeout1->SetValue(wxString::Format("%d",settings->Loop1Timeout));
    textCtrl_Timeout2->SetValue(wxString::Format("%d",settings->Loop2Timeout));
    
    textCtrl_CompsDir->SetValue(settings->ComponentsDirectory);
    checkBox_TerminateComps->SetValue(settings->AutoTerminateComponents);
  }
}

void GimboSettingsFrame::OnOK( wxCommandEvent& event )
{
  TGimboSettings temp;
  long int l1,l2;
  if (settings)
  {
    temp.Autoconnect=checkBox_Autoconnect->IsChecked();
    temp.Hub=comboBox_Hub->GetValue().mb_str(wxConvUTF8);
    
    if (!(textCtrl_Port->GetValue().ToLong( &l1 )))
    {
      wxMessageBox(_T("Port value invalid."));
      return;
    }
    temp.Port=l1;
    
    temp.OwnName=textCtrl_OwnName->GetValue().mb_str(wxConvUTF8);
    
    temp.Loop1Enabled=checkBox_Loop1->IsChecked();
    temp.Loop2Enabled=checkBox_Loop2->IsChecked();
    
    if (!(textCtrl_Timeout1->GetValue().ToLong(&l1 ) ) || 
          !(textCtrl_Timeout2->GetValue().ToLong(&l2 ) ))
    {
      wxMessageBox(_T("Timeout value invalid."));
      return;
    }
    temp.Loop1Timeout=l1;
    temp.Loop2Timeout=l2;
        
    temp.ComponentsDirectory=textCtrl_CompsDir->GetValue();
    temp.AutoTerminateComponents=checkBox_TerminateComps->IsChecked();
    
    *settings=temp;
  }
  wxCommandEvent e(MY_EVT_SETTINGS_CHANGED, GetId());
  GetParent()->AddPendingEvent(e);
  Destroy();
}

void GimboSettingsFrame::OnCancel( wxCommandEvent& event )
{
  Destroy();
}

void GimboSettingsFrame::OnActivate( wxActivateEvent& event )
{
  
}

void GimboSettingsFrame::OnClose( wxCloseEvent& event )
{
  Destroy();
}

